package com.clss.commandhelper.tools;

import android.content.Context;
import android.content.SharedPreferences;

import com.clss.commandhelper.CommandHelperApplication;
import com.clss.commandhelper.function.login.LoginBean;


/**
 * SharedPreferences帮助类，保存用户信息等内容
 *
 * @author LiuV
 * @date 2017/6/12
 */

public class SpHelper {

    private static final String TAG = "SpHelper";

    private static final String PROJECT_NAME = "command_helper";

    private static final String TOKEN = "token";
    private static final String NAME = "name";
    private static final String ID = "id";
    private static final String USER_NAME = "user_name";
    private static final String PHONE = "phone";
    private static final String USER_ID = "user_id";
    private static final String ROLE = "role";
    private static final String IS_VERIFY = "is_verify";
    private static final String BIRTH = "birthday";
    private static final String GROUP_NAME = "group_name";
    private static final String POLICE_ID = "police_id";
    private static final String ICON = "icon";

    private static final String LAST_VERSION_CHECK_TIME = "last_version_check_time";

    private static final String SEARCH_HISTORY = "search_history";

    private static final String SPLASH_AD_URL = "splash_ad_url";
    private static final String SPLASH_CONTENT_URL = "splash_content_url";
    private static final String BANNER_URL = "banner_url";
    private static final String LOGIN_COUNT = "login_count";


    private SharedPreferences mSharedPreferences;

    private static SpHelper spHelper;

    public static SpHelper getInstance(){
        if (spHelper == null){
            synchronized (SpHelper.class){
                if (spHelper == null){
                    spHelper = new SpHelper();
                    return spHelper;
                }
            }
        }
        return spHelper;
    }

    private SpHelper(){
        this(CommandHelperApplication.getAppContext(), Context.MODE_PRIVATE);
    }

    private SpHelper(Context context, int mode){
        mSharedPreferences = context.getSharedPreferences(PROJECT_NAME, mode);
    }

    public boolean putString(String key, String value){
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString(key, value);
        return editor.commit();
    }

    public String getString (String key){
        return mSharedPreferences.getString(key, "");
    }

    public boolean putInt(String key, int value){
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putInt(key, value);
        return editor.commit();
    }

    public int getInt (String key){
        return mSharedPreferences.getInt(key, -1);
    }

    public boolean putLong(String key, long value){
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putLong(key, value);
        return editor.commit();
    }

    public long getLong (String key){
        return mSharedPreferences.getLong(key, -1);
    }

   public String getToken(){
        return getString(TOKEN);
    }
    public boolean setToken(String value){
        return putString(TOKEN, value);
    }


//    public boolean setName(String name){
//        return putString(USER_NAME, name);
//    }

   /* public String getName(){
        return getString(USER_NAME);
    }*/

//    public boolean setPhone(String phone){
//        return putString(PHONE, phone);
//    }

    /*public String getPhone(){
        return getString(PHONE);
    }

    public String getUserId() {
        return getString(USER_ID);
    }*/

//    public boolean setUserId(String userId){
//        return putString(USER_ID, userId);
//    }

    public void saveAll(LoginBean bean){
        putString(TOKEN, bean.getToken());
        putString(USER_NAME, bean.getUserName());
        putString(PHONE, bean.getPhone());
        putString(USER_ID, bean.getUserId());
        putString(NAME, bean.getName());
        putString(ID, bean.getId());
        putString(BIRTH, bean.getBirthday());
        putInt(IS_VERIFY, bean.getIsVerify());
        putString(GROUP_NAME, bean.getGroupname());
        putString(POLICE_ID, bean.getPoliceId());
        putString(ICON, bean.getIcon());
        putInt(ROLE, bean.getRole());
    }

    public void clearAll(){
        LoginBean loginBean = new LoginBean();
        loginBean.setToken("");
        loginBean.setUserName("");
        loginBean.setPhone("");
        loginBean.setUserId("");
        loginBean.setName("");
        loginBean.setId("");
        loginBean.setBirthday("");
        loginBean.setIsVerify(-1);
        loginBean.setPoliceId("");
        loginBean.setGroupname("");
        loginBean.setIcon("");
        loginBean.setRole(-1);
        saveAll(loginBean);
    }

    public boolean setIsVerify(int isVerify){
        return putInt(IS_VERIFY, isVerify);
    }

    public boolean setName(String name){
        return putString(NAME, name);
    }

    public int getIsVerify(){
        return getInt(IS_VERIFY);
    }

    public String getUserId(){
        return getString(USER_ID);
    }

    public String getUserName(){
        return getString(USER_NAME);
    }

    public String getName(){
        return getString(NAME);
    }

    public int getRole(){
        return getInt(ROLE);
    }

    public String getPhone(){
        return getString(PHONE);
    }

    public String getBirth(){
        return getString(BIRTH);
    }

    public long getLastVersionCheckTime(){
        return getLong(LAST_VERSION_CHECK_TIME);
    }

    public void setLastVersionCheckTime(long time){
        putLong(LAST_VERSION_CHECK_TIME, time);
    }


    public boolean setSplashAdUrl(String splashAdUrl){
        return putString(SPLASH_AD_URL, splashAdUrl);
    }

    public String getSplashAdUrl(){
        return getString(SPLASH_AD_URL);
    }

    public boolean setSplashContentUrl(String splashContentUrl){
        return putString(SPLASH_CONTENT_URL, splashContentUrl);
    }

    public String getSplashContentUrl(){
        return getString(SPLASH_CONTENT_URL);
    }

    public boolean setBannerUrl(String bannerUrl){
        return putString(BANNER_URL, bannerUrl);
    }

    public String getBannerUrl(){
        return getString(BANNER_URL);
    }

    public String getGroupName() {
        return getString(GROUP_NAME);
    }

    public String getPoliceId() {
        return getString(POLICE_ID);
    }
    public boolean setIcon(String icon){
        return putString(ICON, icon);
    }

    public String getIcon() {
        return getString(ICON);
    }
}
